
#定义菜单
actions = {}
permissionLink = {}

trunkMenu = {
                    'user':{'permission':100,'lang':'menu_user_manage'},
                    'op':{'permission':200,'lang':'menu_op_support'},
                    'pay':{'permission':300,'lang':'menu_pay_manage'},
                    'stat':{'permission':400,'lang':'menu_stat_manage'},
            }

branchMenu = {
        'user':{
                        'userinfo':{'permission':101,'lang':'menu_user_userinfo'},
                        'building':{'permission':102,'lang':'menu_user_building'},
                        'army':{'permission':103,'lang':'menu_user_army'},
                        'goods':{'permission':104,'lang':'menu_user_goods'},

        },
        'op':{
                        'push':{'permission':201,'lang':'menu_op_push'},
                        'announce_mail':{'permission':202,'lang':'menu_op_announce_mail'},
                        'usermail':{'permission':203,'lang':'menu_op_usermail'},
                        'refund':{'permission':204,'lang':'menu_op_refund'},
        },
        'pay':{
                        'detail':{'permission':301,'lang':'menu_pay_detail'},
                        'analyze':{'permission':302,'lang':'menu_pay_pay_analyze'},
                        'roi':{'permission':303,'lang':'menu_pay_roi'},
        },
        'stat':{
                        'dau':{'permission':401,'lang':'menu_stat_dau'},
                        'registerdata':{'permission':402,'lang':'menu_stat_register'},
                        'regremain':{'permission':403,'lang':'menu_stat_regremain'},

        },
}

"""
         * see 生成导航菜单
         *
         * param array groupid
         *
         * param array permission
         *
         * return array
         *
"""
def initMenu(groupid, permission = False):

    supers = True if int(groupid/10) == 1 else False
    hidePermission = True if int(groupid/10) == 2 else False

    if not supers and  not permission:
        return

    menu = {}
    menu['sub_menu'] = {}
    for tab, value in trunkMenu.items():
        if supers or value['permission'] in permission:
            trunkPermission = True
            branchPermission = True
        else:
            branchPermission = False
            trunkPermission = False
            for subTab, subValue in branchMenu[tab].items():
                    if permission[subValue['permission']]:
                            trunkPermission = True
        if trunkPermission :
            menu[tab] = {"name" :value['lang'],"permit" : value['permission']}

            for subTab, subValue in branchMenu[tab].items():
                    if 'hide' in subValue.keys() and not hidePermission and not supers:
                            continue
                    if branchPermission or permission[subValue['permission']]:
                            menu['sub_menu'][subValue['permission']]={
                              "name" : subValue['lang'],
                              "permit" : subValue['permission'],
                              "href" : "%s_%s" % (tab, subTab),
                              "action": subTab,
                              }

    return menu


if __name__ == "__main__":
    menus = initMenu(31, [100,200,300,400])
    print(menus['sub_menu'])
